//
//  GCNumberFomatter.m
//  gamco-ios
//
//  Created by 昭荣伊 on 15/12/22.
//  Copyright © 2015年 onemt. All rights reserved.
//

#import "OMTNumberFormatter.h"
#import <UIKit/UIKit.h>

#define DEFAULT_LOCALE @"en"

// 一千
static CGFloat const OneThousand = 1000.0;
// 一百万
static CGFloat const OneMillion = 1000 * 1000.0;

static CGFloat const kOneKB = 1024.0;
static CGFloat const kOneMB = kOneKB * kOneKB;
static CGFloat const kOneGB = kOneKB * kOneKB *kOneKB;

@implementation OMTNumberFormatter

+ (NSString *)formatWithNumber:(NSNumber *)number {
    return [self formatWithNumber:number localeIdentifier:DEFAULT_LOCALE];
}

+ (NSString *)formatWithNumber:(NSNumber *)number localeIdentifier:(NSString *)localeIdentifier {
    if (number.floatValue < 1) return @"";
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
    [numberFormatter setLocale:locale];
    if (number.floatValue >= OneThousand) {
        numberFormatter.positiveFormat = @"###,###.0;";
    }else {
        numberFormatter.positiveFormat = @"###,###;";
    }
    if (number.floatValue >= OneMillion) {
        numberFormatter.positiveSuffix = @"M";
        return [numberFormatter stringFromNumber:@(number.floatValue / OneMillion)];
    }else if (number.floatValue >= OneThousand) {
        numberFormatter.positiveSuffix = @"K";
        return [numberFormatter stringFromNumber:@(number.floatValue / OneThousand)];
    }
    return [numberFormatter stringFromNumber:number];
}

+ (NSString *)formatWithInteger:(NSInteger )number {
    return [self formatWithNumber:@(number)];
}

+ (NSString *)formatWithInteger:(NSInteger )number localeIdentifier:(NSString *)localeIdentifier {
    return [self formatWithNumber:@(number) localeIdentifier:localeIdentifier];
}

+ (NSString *)formatOrginalWithInterger:(NSInteger)number {
    return [self formatOrginalWithInterger:number localeIdentifier:DEFAULT_LOCALE];
}

+ (NSString *)formatOrginalWithInterger:(NSInteger)number localeIdentifier:(NSString *)localeIdentifier {
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
    [numberFormatter setLocale:locale];
    return [numberFormatter stringFromNumber:@(number)];
}

//原始的格式化数字,不进行K,M换算,添加上,
+ (NSString *)formatCommaOrginalWithInterger:(NSInteger)number {
    return [self formatCommaOrginalWithInterger:number localeIdentifier:DEFAULT_LOCALE];
}
+ (NSString *)formatCommaOrginalWithInterger:(NSInteger)number localeIdentifier:(NSString *)localeIdentifier {
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
    [numberFormatter setLocale:locale];
    numberFormatter.positiveFormat = @"###,###;";
    return [numberFormatter stringFromNumber:@(number)];
}



+ (NSString *)fileSizeWithInterger:(NSUInteger)size {
    
    return [self fileSizeWithInterger:size localeIdentifier:DEFAULT_LOCALE];
}

+ (NSString *)fileSizeWithInterger:(NSUInteger)size localeIdentifier:(NSString *)localeIdentifier {
    NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
    [numberFormatter setLocale:locale];
    
    NSString *resultString = @"";
    if (size < kOneMB){// 小于1m
        CGFloat aFloat = size/kOneKB;
        numberFormatter.positiveFormat = @"###,###;";
        resultString = [NSString stringWithFormat:@"%@ KB",[numberFormatter stringFromNumber:@(aFloat)]];
    }else if (size < kOneGB){// 小于1G
        numberFormatter.positiveFormat = @"###,###.0;";
        CGFloat aFloat = size/kOneMB;
        resultString = [NSString stringWithFormat:@"%@ MB",[numberFormatter stringFromNumber:@(aFloat)]];
    }else{
        numberFormatter.positiveFormat = @"###,###.00;";
        CGFloat aFloat = size/kOneGB;
        resultString = [NSString stringWithFormat:@"%@ GB",[numberFormatter stringFromNumber:@(aFloat)]];
    }
    return resultString;
}

@end
